package com.tea.tea_vue.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tea.tea_vue.Entity.Sells;
import com.tea.tea_vue.Mapper.SellsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SellService {

    @Autowired
    private SellsMapper sellsMapper;

    public String getSellList(Integer pageNum, Integer pageSize) throws JsonProcessingException, JSONException {
        PageHelper.startPage(pageNum, pageSize);
        List<Sells> sells = sellsMapper.selectAll();
        PageInfo<Sells> pageInfo = new PageInfo<>(sells, pageNum);
        ObjectMapper objectMapper = new ObjectMapper();
        String sellsList = objectMapper.writeValueAsString(sells);
        //System.out.println(userList);
        JSONArray jsonArray = new JSONArray(sellsList);
        JSONObject jsonPage = new JSONObject();
        jsonPage.accumulate("pageNum", pageNum);
        jsonPage.accumulate("pageCount", pageInfo.getPages());
        jsonArray.put(jsonPage);
        sellsList = jsonArray.toString();
        //System.out.println(userList);
        return sellsList;
    }
}
